package com.dts.backproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dts.backproject.entity.po.Video;
import com.dts.backproject.mapper.VideoMapper;
import com.dts.backproject.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<Video> findAll() {
        List<Video> Videos = videoMapper.selectList(null);
        return Videos;
    }
    
    @Override
    public Video getVideoById(String id) {
        return videoMapper.selectById(id);
    }
    
    @Override
    public void addVideo(Video video) {
        if (video.getCreateAt() == null) {
            video.setCreateAt(LocalDateTime.now());
        }
        if (video.getUpdateAt() == null) {
            video.setUpdateAt(LocalDateTime.now());
        }
        if (video.getStatus() == null) {
            video.setStatus("active");
        }
        videoMapper.insert(video);
    }
    
    @Override
    public void updateVideo(Video video) {
        video.setUpdateAt(LocalDateTime.now());
        videoMapper.updateById(video);
    }
    
    @Override
    public void deleteVideo(String id) {
        Video video = videoMapper.selectById(id);
        if (video != null) {
            video.setStatus("deleted");
            video.setUpdateAt(LocalDateTime.now());
            videoMapper.updateById(video);
        }
    }
    
    @Override
    public List<Video> getVideosByUploader(String email) {
        LambdaQueryWrapper<Video> wrapper = new QueryWrapper<Video>().lambda()
                .eq(Video::getUploaderEmail, email)
                .eq(Video::getStatus, "active")
                .orderByDesc(Video::getCreateAt);
        return videoMapper.selectList(wrapper);
    }
}

